﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;

namespace app.Api
{
    public class ApiArticle: ApiBase
    {
        public ApiArticle(Controller ctx) : base(ctx) { }

        #region 自定义请求参数
        // 作者
        string req_author { get { return GetStringParam(new ReqStringParamOption{ Name = "author", Level= REQPARAMLEVEL.FREE }); } }
        string req_m_type { get { return GetStringParam(new ReqStringParamOption { Name = "m_type", Level = REQPARAMLEVEL.FREE, DefaultValue = "" }); } }
        string req_a_type { get { return GetStringParam(new ReqStringParamOption { Name = "a_type", Level = REQPARAMLEVEL.FREE, DefaultValue = "" }); } }
        string req_p_type { get { return GetStringParam(new ReqStringParamOption { Name = "p_type", Level = REQPARAMLEVEL.FREE, DefaultValue = "" }); } }
        #endregion

        public void Do(string action)
        {
            switch (action)
            {
                case "get":
                    {
                        Cd cd = new Cd("id={0}", req_id);
                        if (UserIdentity != IDENTITY.ADMIN)
                        { // 如果不是管理员，则只能看到公开的文章
                            cd.And("status=1");
                        }
                        Article a = G.DB.Get<Article>(cd.Render()).GetFirstEntity();

                        if (a != null && UserIdentity != IDENTITY.ADMIN)
                        { // 如果不是管理员访问，则访问量加一
                            a.click_num += 1;
                            G.DB.Update<Article>(a);
                        }

                        ResponseJson(new ObjectResult { data = a });
                        return;
                    }
            }

            CheckPermission(IDENTITY.ADMIN);
            switch (action)
            {
                case "status": // 设置状态
                    {
                        CheckPermission(IDENTITY.ADMIN, Permission.Module(PERMISSIONMODULE.INFO_LIST).Modify());
                        Article a = G.DB.GetById<Article>(req_id);
                        a.status = req_status;
                        G.DB.Update<Article>(a);
                        ResponseJson(new Result());
                        break;
                    }
                case "top":
                    {
                        CheckPermission(IDENTITY.ADMIN, Permission.Module(PERMISSIONMODULE.INFO_LIST).Modify());
                        Article a = G.DB.GetById<Article>(req_id);
                        a.is_top = req_status;
                        G.DB.Update<Article>(a);
                        ResponseJson(new Result());
                        break;
                    }
                case "add":
                    {
                        CheckPermission(IDENTITY.ADMIN, Permission.Module(PERMISSIONMODULE.INFO_LIST).Add());
                        Article a = new Article();
                        ApplyValues(a);
                        if (G.DB.Insert<Article>(a) == 0) ThrowStatus(STATUS.FAILED_INSERT);
                        else ResponseJson(new IDResult { id = a.id });
                        break;
                    }
                case "modify":
                    {
                        CheckPermission(IDENTITY.ADMIN, Permission.Module(PERMISSIONMODULE.INFO_LIST).Modify());
                        Article a = G.DB.GetById<Article>(req_id);
                        ApplyValues(a);
                        if (G.DB.Update<Article>(a) == 0) ThrowStatus(STATUS.FAILED_UPDATE);
                        else ResponseJson(new Result());
                        break;
                    }
                case "delete":
                    {
                        CheckPermission(IDENTITY.ADMIN, Permission.Module(PERMISSIONMODULE.INFO_LIST).Delete());
                        if (G.DB.Delete<Article>(req_id) == 0) ThrowStatus(STATUS.FAILED_DELETE);
                        else ResponseJson(new Result());
                        break;
                    }
                default: break;
            }

        }

        private void ApplyValues(Article a)
        {
            a.title = req_title;
            a.author = UserName;
            a.create_time = DateTimeEx.GetNowTimeStamp();
            a.content = req_content;
            a.video_ids = GetStringParam(new ReqStringParamOption { Name = "video_ids" });
            a.m_type = req_m_type;
            a.p_type = req_p_type;
            a.a_type = req_a_type;
        }
    }


}